package it.unipi.di.rasip.gui.chat;

import it.unipi.di.rasip.utils.Color;

import java.util.Enumeration;
import java.util.Vector;

import javax.microedition.lcdui.CustomItem;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;

public class ChatPanel extends CustomItem {
	int width;
	int height;
	
	Vector messageList;
	
	public ChatPanel(int width, int height, Vector mList) {
		super(null);
		this.width = width;
		this.height = height;
		
		this.messageList = mList;
	}
	
	protected int getMinContentHeight() {
		return this.height;
	}

	protected int getMinContentWidth() {
		return this.width;
	}

	protected int getPrefContentHeight(int arg0) {
		return this.height;
	}

	protected int getPrefContentWidth(int arg0) {
		return this.width;
	}

	protected void paint(Graphics g, int w, int h) {
		Font f = Font.getDefaultFont();
		g.setColor(Color.WHITE.getCode());
		g.fillRect(0, 0, w, h);
		
		int y = h - 3;
		int y_inc = f.getHeight();
		//System.out.println("Repaint messages: "+messageList.size());
		//System.out.println("Y:"+y);
		Enumeration e = messageList.elements();
		while (e.hasMoreElements() && y > 0) {
			ChatMessage msg = (ChatMessage) e.nextElement();
			//y -= y_inc;
			y = msg.drawMessage(g, f, 0, y, this.width-5);
		}
	}
	
	public void redraw() {
		this.invalidate();
	}
}
